package com.party.web.web.controller.wechat;

import com.party.core.model.member.MemberGroup;
import com.party.core.service.wechat.IWechatMenuService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.web.biz.wechat.WechatMenuBizService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.AjaxResult;
import com.party.web.web.dto.input.wechat.WechatMenuInputWrapper;
import com.party.web.web.dto.output.wechat.WechatMenuOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static com.party.web.utils.RealmUtils.getCurrentUser;

/**
 * 微信菜单控制器
 * Created by wei.li
 *
 * @date 2017/8/29 0029
 * @time 17:14
 */

@Controller
@RequestMapping(value = "wechat/menu")
public class WechatMenuController {

    @Autowired
    private WechatMenuBizService wechatMenuBizService;

    @Autowired
    private IWechatMenuService wechatMenuService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    protected static Logger logger = LoggerFactory.getLogger(WechatMenuController.class);

    /**
     * 进入自定义菜单页
     * @return 视图模型
     */
    @RequestMapping("view")
    public ModelAndView menuView(){
        //授权判定
        MemberGroup member = RealmUtils.getCurrentUser();
        boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
        if(!isAuthorize){
            //没有授权
            return new ModelAndView("wechat/boundTips");
        }
        ModelAndView mv = new ModelAndView("wechat/menuMgr");
        return mv;
    }

    /**
     * 树结构列表
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "treeList")
    public AjaxResult treeList(){
        AjaxResult ajaxResult = new AjaxResult();
        MemberGroup member = getCurrentUser();
        List<WechatMenuOutput> list = wechatMenuBizService.treeList(member.getId());
        ajaxResult.setData(list);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 创建菜单
     * @param inputWrapper
     * @return
     */
    @ResponseBody
    @RequestMapping("create")
    public AjaxResult createMenu(@RequestBody WechatMenuInputWrapper inputWrapper){
        AjaxResult ajaxResult = new AjaxResult();
        //先保存生成KEY，插入数据库直接删除当前所有的，再插入新的，上传至服务器，最后上传至服务器
        String memberId = RealmUtils.getCurrentUser().getId();
        try {
            wechatMenuBizService.create(inputWrapper.getMenuList(),memberId);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            logger.error("生成菜单异常——用户id:" + memberId, e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription(e.getMessage());
        }
        return ajaxResult;
    }

    /**
     * 删除整个菜单
     */
    @ResponseBody
    @RequestMapping("delAllMenu")
    public AjaxResult delAllMenu(){
        AjaxResult ajaxResult = new AjaxResult();
        boolean opFlag = wechatMenuBizService.delAllMenu();
        if(opFlag){
            ajaxResult.setSuccess(true);
        }else{
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }
}
